package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Copyright: Copyright (c) 2019 Super520
 * 描述：
 *
 * @package: cn.wolfcode.trip.admin.controller
 * @author: Patch
 * @date: 2019/5/22 14:59
 * 总结:
 */
@Controller
@RequestMapping("travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        // 为空才设置
        if (qo.getState() == null) {
            // 待审核状态
            qo.setState(Travel.STATE_PENDING);
        }
        // 公开
        qo.setIsPublic(true);
        // 最后更新时间升序
        qo.setOrderBy("t.lastUpdateTime asc");
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }

    /**
     * 已发布列表
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("releaseList")
    public String releaseList(Model model, @ModelAttribute("qo") TravelQueryObject qo) {
        // 已发布状态
        qo.setState(Travel.STATE_RELEASE);
        // 公开
        qo.setIsPublic(true);
        // 最后更新时间
        qo.setOrderBy("t.lastUpdateTime desc");
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/releaseList";
    }

    /**
     * 获取指定的文章信息
     *
     * @param id
     * @return
     */
    @RequestMapping("getContentById")
    @ResponseBody
    public Object getContentById(Long id) {
        return travelService.getContentById(id);
    }

    /**
     * 更新发布状态
     *
     * @param travel
     */
    @RequestMapping("updateState")
    @ResponseBody
    public Object updateState(Travel travel) {
        JsonResult result = new JsonResult();
        try {
            travelService.updateState(travel);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲，操作失败");
        }
        return result;
    }

}
